package com.analytical.www

import android.graphics.Color
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.analytical.www.base.AppBase
import com.analytical.www.databinding.ActivityMainBinding
import com.analytical.www.fargment.FragHome
import com.analytical.www.fargment.FragSetting
import com.analytical.www.fargment.FragTestResult
import com.analytical.www.util.StringUtils.toast
import com.analytical.www.util.UtilBar
import com.analytical.www.util.UtilTime
import java.util.*

class MainActivity : AppCompatActivity() {
    private lateinit var binding:ActivityMainBinding
    private var isExit = false
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        UtilBar.setStatusBarColor(this, Color.TRANSPARENT)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        initTime()
        changeTitle("运行")
        initClick()
        showContentFrag(FragHome.newInstance())
        changeBgToRun()
    }

    private fun changeBgToRun() {
        binding.ll1.isSelected = false
        binding.ll2.isSelected = false
        binding.ll3.isSelected = true
        binding.ll4.isSelected = false
    }

    private fun initClick(){
        binding.tvSetting.setOnClickListener {
            binding.ll1.isSelected = true
            binding.ll2.isSelected = false
            binding.ll3.isSelected = false
            binding.ll4.isSelected = false
            changeTitle("高级设置")
            changerBtnState(0)
            replaceContentFrag(FragSetting.newInstance())
        }
        binding.tvTestResult.setOnClickListener {
            binding.ll1.isSelected = false
            binding.ll2.isSelected = true
            binding.ll3.isSelected = false
            binding.ll4.isSelected = false
            changerBtnState(0)
            changeTitle("测试结果")
            replaceContentFrag(FragTestResult.newInstance())
        }
        binding.tvRun.setOnClickListener {
            changeBgToRun()
            replaceContentFrag(FragHome.newInstance())
        }
        binding.tvStartTest.setOnClickListener {
            supportFragmentManager.fragments.forEach {
                if (it is FragHome) {
                    if (it.isStartTest()) {
                        changerBtnState(2)
                        it.startTest()
                    } else {
                        "请配置参数，才可以进行测试".toast()
                    }
                }
            }
        }
    }

    private fun initTime() {
        findViewById<TextView>(R.id.tv_time).text = "%s\n%s".format(UtilTime.getCurrentTimeString(),UtilTime.getCurTime())
    }

    /**
     * 修改按钮的状态
     * 1.背景
     * 2.显示文字
     * */
    fun changerBtnState(state: Int) {
        when (state) {
            0 -> {
                binding.ll4.setBackgroundResource(R.drawable.shape_state_ready)
                binding.tvStartTest.text = "新测试"
            }
            1 -> {
                binding.ll4.setBackgroundResource(R.drawable.shape_state_aready)
                binding.tvStartTest.text = "开始测试"
            }
            2 -> {
                binding.ll4.setBackgroundResource(R.drawable.shape_state_run)
                binding.tvStartTest.text = "正在测试"
            }
        }
    }


    fun showFullScreenFrag(frag: Fragment) {
        supportFragmentManager.beginTransaction()
            .add(R.id.fl_full_content, frag, frag::class.java.name)
            .commitAllowingStateLoss()
    }

    private fun showContentFrag(frag: Fragment) {
        supportFragmentManager.beginTransaction()
            .add(R.id.fl_content, frag, frag::class.java.name)
            .commitAllowingStateLoss()
    }

    private fun replaceContentFrag(frag: Fragment) {
        supportFragmentManager.beginTransaction()
            .replace(R.id.fl_content, frag, frag::class.java.name)
            .commitAllowingStateLoss()
    }

    fun removeFullScreenFrag(tag: String) {
        supportFragmentManager.findFragmentByTag(tag)?.let {
            supportFragmentManager.beginTransaction()
                .remove(it)
                .commitAllowingStateLoss()
        }
    }

    fun changeTitle(title:String) {
        findViewById<TextView>(R.id.tv_titile).text = title
    }

    override fun onBackPressed() {
        if (AppBase.instance.dispatcherOnKeyCodeBack()) {
            return
        }
        val tExit: Timer?
        if (!isExit) {
            "连续按两次退出应用~".toast()
            isExit = true // 准备退出
            tExit = Timer()
            tExit.schedule(object : TimerTask() {
                override fun run() {
                    isExit = false // 取消退出
                }
            }, 2000) // 如果2秒钟内没有按下返回键，则启动定时器取消掉刚才执行的任务
        } else {
            finish()
        }
    }
}
